export let baseUrl = "https://www.qst-sf.com";

export const uploadUrl = 'https://www.qst-sf.com' + '/rest/user/center/upload';

Promise.prototype.finally = function (callback) {
  var Promise = this.constructor;
  return this.then(
    function (value) {
      Promise.resolve(callback()).then(
        function () {
          return value;
        }
      );
    },
    function (reason) {
      Promise.resolve(callback()).then(
        function () {
          throw reason;
        }
      );
    }
  );
}
const request=(data)=>{
	wx.showLoading({
	  title: 'Loading...', 
	  mask: true,
	});
	if(!data.method){
		data.method='POST'
	}
	
	let type;

	if(data.type == 2){
		type = 'application/json;charset=UTF-8';
	}else{
		type = 'application/x-www-form-urlencoded'
	}

	let header={
		'content-type': type,
		'accept':'application/json'
	}
	if(data.isLogin != 2){
		let userInfo = wx.getStorageSync('userInfo');
		if(userInfo){
			header['userId'] = userInfo.userId;
		}
		let isDisable = wx.getStorageSync('isDisable');

		if(isDisable !== '' && isDisable !== null && isDisable !== undefined){
			header['status'] = isDisable;
			
		}
	}
	let obj={}
	
	if(data.type == 2){
		data.data = JSON.stringify(data.data);
	}else{
		data.data = Object.assign(obj,data.data);
	}
		

	console.log(data.data);
	// console.log(header);
	return new Promise((resolve, reject) => {
		wx.request({
			url: baseUrl+data.url,
			data:data.data,
			header,
			
			method: data.method ? data.method : 'GET',
			success: function (res) {
				wx.hideLoading()
				if (res.statusCode == 200) {

					if(res.data && res.data.code == 301){

						wx.showToast({
							title: res.data.msg,
							duration: 1000,
							icon:"none"
						})
						// resolve(res.data);
					}else{
						resolve(res.data);
					}
					
				} else {
					wx.showToast({
						title:'哎呀，开小差了~',
						icon:'none'
					})
					reject(res.data.msg); //返回错误提示信息
				}
			},
			fail: function (res) {
				wx.hideLoading()
				wx.showToast({
					title:'哎呀，开小差了~',
					icon:'none'
				})
				reject("网络连接错误"); //返回错误提示信息
			},
			complete: function (res) {

			}
		})
	});
}
export default request;